package com.gct.service;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.gct.domain.Theme;
import com.gct.persistence.ThemeMapper;
import com.gct.service.base.BaseService;

@Service
public class ThemeService extends BaseService {
    @Autowired
    private ThemeMapper themeMapper;

    public List<Theme> getThemes() {
        return themeMapper.getThemes();
    }

    public Theme getArtsInTheme(int id) {
        Theme theme = new Theme();
        theme.setId(id);
        return themeMapper.getArtsInTheme(theme);
    }

    public Theme getArtsInThemeWithDuration(int id, int seconds) {
        Theme theme = new Theme();
        theme.setId(id);
        return themeMapper.getArtsInThemeWithDuration(theme, seconds);
    }

    @Transactional(isolation=Isolation.READ_COMMITTED)
    public void voteArtThemes(long artId, List<Integer> themeIds) {
        List<Theme> themes = getThemesByIds(artId,themeIds);
        List<Integer> themesToVote = new LinkedList<Integer>();
        if(themes.size() > 0)
            for(Theme t : themes)
                themesToVote.add((int)t.getId());

        themeIds.removeAll(themesToVote);
        if(0 < themeIds.size())
            themeMapper.themeArt(artId,themeIds);
    }

    private List<Theme> getThemesByIds(long artId, List<Integer> themeIds) {
        return themeMapper.getThemesByIds(artId, themeIds);
    }
}
